package me.nibo.horizontal.listview;

import android.support.v7.app.ActionBarActivity;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import it.sephiroth.android.library.widget.AbsHListView;
import me.nibo.horizontal.listview.model.Cert;

/**
 * @author nibo
 */
public class HListViewSecondAdapter extends BaseAdapter {
    private List<Cert> dataList;
    private ActionBarActivity mContext;
    private LayoutInflater mInflater;
    private int selectIndex = -1;

    public HListViewSecondAdapter(ActionBarActivity context, List<Cert> dataList) {
        this.mContext = context;
        this.dataList = dataList;
        mInflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.item_second_list, null);
            holder.hLayout = (LinearLayout) convertView.findViewById(R.id.layout_hlist);
            holder.mImage = (ImageView) convertView.findViewById(R.id.img_pic);
            holder.mTitle = (TextView) convertView.findViewById(R.id.tv_cert_name);
            holder.tvCountTime = (TextView) convertView.findViewById(R.id.tv_count_time);
            holder.tvLearnTime = (TextView) convertView.findViewById(R.id.tv_learn_time);
            holder.tvPro = (TextView) convertView.findViewById(R.id.tv_pro);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (position == selectIndex) {
            convertView.setSelected(true);
        } else {
            convertView.setSelected(false);
        }

        Cert cert = dataList.get(position);
        holder.mTitle.setText(cert.getName());
        holder.tvCountTime.setText(cert.getCountTime());
        holder.tvLearnTime.setText(cert.getLearnTime());
        holder.tvPro.setText(cert.getPro());

        DisplayMetrics dm = new DisplayMetrics();
        //获取屏幕信息
        mContext.getWindowManager().getDefaultDisplay().getMetrics(dm);

        int screenWidth = dm.widthPixels;

        holder.hLayout.setLayoutParams(new AbsHListView.LayoutParams(
                screenWidth,
                it.sephiroth.android.library.widget.AbsHListView.LayoutParams.WRAP_CONTENT));

        return convertView;
    }

    private static class ViewHolder {
        private ImageView mImage;
        private LinearLayout hLayout;
        private TextView mTitle;
        private TextView tvCountTime;
        private TextView tvLearnTime;
        private TextView tvPro;
    }


    public void setSelectIndex(int i) {
        selectIndex = i;
    }
}
